<template>
  <div class="app-container">
    <el-form ref="placeCodeForm" v-loading="formLoading" :rules="rules" :model="form" label-position="top">
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="封面" prop="coverUrl" style="width: 800px">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="form.coverUrl" :src="form.coverUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="标题" prop="title" style="width: 400px">
            <el-input v-model="form.title" placeholder="请输入标题" clearable  />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="开始时间" prop="liveTime" style="width: 400px">
            <el-date-picker
              v-model="liveTime"
              value-format="yyyy-MM-dd HH:mm:ss"
              size="small"
              clearable
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              align="left"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="选择企业" prop="comIds" style="width: 400px">
            <el-select v-model="form.comIds" filterable multiple placeholder="请选择">
              <el-option
                v-for="item in comList"
                :key="item.id"
                :label="item.enterpriseName"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="选择用户" prop="userId" style="width: 400px">
            <el-select v-model="form.userId" filterable placeholder="请选择">
              <el-option
                v-for="item in userList"
                :key="item.id"
                :label="item.realName"
                :value="item.userId">
                <span style="float: left">{{ item.realName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.comName }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="选择岗位" prop="publishIds" style="width: 400px">
            <el-select v-model="form.publishIds" filterable multiple placeholder="请选择">
              <el-option
                v-for="item in publishList"
                :key="item.id"
                :label="item.jobTitle"
                :value="item.id">
                <span style="float: left">{{ item.jobTitle }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.comName }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" :gutter="20">
        <el-col :span="24">
          <el-form-item label="介绍" prop="introduce">
            <el-input type="textarea" v-model="form.introduce" placeholder="请输入介绍" :rows="5"  />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import {
  editLivePortendApi,
  geLivePortendByIdApi,
  getComListApi,
  getComUserListApi,
  publishListApi
} from "@/api/livePortend";
export default {
  components: { Editor, Toolbar },
  name: 'PlaceCodeDetail',
  data(){
    return{
      editor: null,
      liveTime:[],
      comList:{},
      userList:{},
      publishList:{},
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      toolbarConfig: {
        excludeKeys: [
          // 排除菜单组，写菜单组 key 的值即可
          'group-video' // 去掉视频
        ]
      },
      editorConfig: { MENU_CONF: {}, placeholder: '' },
      mode: 'default',
      uploadUrl: process.env.VUE_APP_UPLOAD_API,
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        id: null,
        title:'',
        startTime: '',
        endTime: '',
        introduce:'',
        userId: '',
        comIds:'',
        coverUrl: '',
        publishIds: ''
      },

      rules: {
        title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        // liveTime: [{ required: true, trigger: 'blur', message: '请选择开始时间' }],
        introduce: [{ required: true, trigger: 'blur', message: '请填写简介' }],
        userId: [{ required: true, trigger: 'blur', message: '请选择开播用户' }],
        coverUrl:[{ required: true, trigger: 'blur', message: '请选择封面' }],
        // publishIds:[{ required: true, trigger: 'blur', message: '请选择岗位' }],
        comIds:[{ required: true, trigger: 'blur', message: '请选择企业' }],
      },
      formLoading: false,
    }
  },
  props:['id','closeDialog'],
  methods:{
    handleAvatarSuccess(res, file) {
      this.form.coverUrl = res.data;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    submit(){
      this.$refs.placeCodeForm.validate(valid => {
        if (valid) {
          const loading=this.$loading({
            text:'提交中'
          })
          if(this.form.id==null)
          {
            this.form.id = 0;
          }
          const form=JSON.parse(JSON.stringify(this.form));
          form.comIds=form.comIds.length>0?form.comIds.join(','):'';
          form.publishIds = form.publishIds.length>0?form.publishIds.join(','):null;
          if(this.liveTime!=null)
          {
            form.startTime = this.liveTime[0];
            form.endTime = this.liveTime[1];
          }
          editLivePortendApi(form).then((res)=>{
            this.$message.success('保存成功');
            this.closeDialog(0);
          }).catch(()=>{
            this.$message.error('保存失败');
          }).finally(()=>{
            loading.close();
          })
        } else {
          return false
        }
      })
    },
    getComList()
    {
      getComListApi().then((res)=>{
      this.comList=res;
    })
    },
    getComUserList(comIds)
    {
      if(comIds.length>0)
      {
        comIds=comIds.length>0?comIds.join(','):'';
        getComUserListApi({comIds}).then((res)=>{
          this.userList=res;
        })
      }

    },
    getComPublishList(comIds)
    {
      if(comIds.length>0)
      {
        comIds=comIds.length>0?comIds.join(','):'';
        publishListApi({comIds}).then((res)=>{
          this.publishList=res;
        })
      }
    },
    getDetail(){
      geLivePortendByIdApi({id:this.id}).then((res)=>{
        res.comIds= res.comIds.split(",");
        for (let i = 0; i < res.comIds.length; i++) {
          res.comIds[i] = parseInt(res.comIds[i]);
        }
       if(res.publishIds)
       {
         res.publishIds = res.publishIds.split(",");
         for (let i = 0; i < res.publishIds.length; i++) {
           res.publishIds[i] = parseInt(res.publishIds[i]);
         }
       }
        this.form=res;
        this.liveTime.push(this.form.startTime)
        this.liveTime.push(this.form.endTime)
        console.log(this.liveTime)
      }).finally(()=>{
        this.formLoading=false;
      })
    },
  },
  watch: {
    "form.comIds"(val) {
      this.getComUserList(val);
      this.getComPublishList(val);
    }
  },
  mounted() {
    if(this.id){
      this.getDetail();
    }
    this.getComList();
  },

}
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #c5c5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

}
.w-e-bar.w-e-bar-show.w-e-toolbar{
  border: 1px solid;
}
.w-e-scroll{
  border: 1px solid;
}
</style>
<style scoped>
/*
*让select撑开
*/
.el-select,.el-cascader{
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #c5c5c5;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
